#include "svuv.hpp"

svuv::svuv(/* args */)
{
    int iret;
    iret = uv_loop_init(&gloop);
    if (iret) 
    {
        PlayerInfo("uv_loop_init", iret);
    }
    
}

svuv::~svuv()
{
}

/*******************************************************Global*******************************************************************************************/
void svuv::gStartLoop()
{
    int iret = uv_run(&gloop, UV_RUN_DEFAULT);
    if (iret) 
    {
        PlayerInfo("uv_run", iret);
    }
}

/*******************************************************TCP Client***************************************************************************************/
int32_t svuv::gaddTcpClient(std::string server_ip, uint16_t server_port, std::string local_ip, uint16_t local_port)
{
    class tcpclient *tmpClient = new tcpclient(&this->gloop, server_ip, server_port, local_ip, local_port);

    this->gtcpClientVec.push_back(tmpClient);

    return gtcpClientVec.size();
}

int32_t svuv::gTcpClientSend(std::string local_ip, uint16_t local_port, uint8_t *sdata, int32_t sdata_len)
{
    std::vector<class tcpclient *>::iterator iter;
    int32_t ret = 0;

    for (iter = gtcpClientVec.begin(); iter != gtcpClientVec.end(); iter++)
    {
        if (((*iter)->tcGetIP() == local_ip) && ((*iter)->tcGetPort() == local_port))
        {
            ret = (*iter)->tcSendData(sdata, sdata_len);
            break;
        }
    }

    return ret;
}

int32_t svuv::gTcpClientRecv(std::string local_ip, uint16_t local_port, uint8_t *buff, int32_t buff_len)
{
    std::vector<class tcpclient *>::iterator iter;
    int32_t ret = 0;

    for (iter = gtcpClientVec.begin(); iter != gtcpClientVec.end(); iter++)
    {
        if (((*iter)->tcGetIP() == local_ip) && ((*iter)->tcGetPort() == local_port))
        {
            ret = (*iter)->tcRecvData(buff, buff_len);
            break;
        }
    }

    return ret;
}

bool svuv::gTcpClientGetConnectStatus(std::string local_ip, uint16_t local_port)
{
    std::vector<class tcpclient *>::iterator iter;
    bool connectStatus = false;

    for (iter = gtcpClientVec.begin(); iter != gtcpClientVec.end(); iter++)
    {
        if (((*iter)->tcGetIP() == local_ip) && ((*iter)->tcGetPort() == local_port))
        {
            connectStatus = (*iter)->tcGetConnectStatus();
            break;
        }
    }

    return connectStatus;
}

/*******************************************************TCP Server***************************************************************************************/
int32_t svuv::gaddTcpServer(std::string server_ip, uint16_t server_port)
{
    this->gtcpServer = new tcpserver(&this->gloop, server_ip, server_port);

    return 0;
}

int32_t svuv::gTcpServerRecv(std::string client_ip, uint16_t client_port, uint8_t *buff, int32_t buff_len)
{
    return gtcpServer->tsGetAcceptClientRecvData(client_ip, client_port, buff, buff_len);
}

int32_t svuv::gTcpServerSend(std::string client_ip, uint16_t client_port, uint8_t *sdata, int32_t sdata_len)
{
    return gtcpServer->tsSetAcceptClientSendData(client_ip, client_port, sdata, sdata_len);
}

int32_t svuv::gTcpServerGetRemainSpace(std::string client_ip, uint16_t client_port)
{
    return gtcpServer->tsGetAcceptClientRemainSpace(client_ip, client_port);
}